package cn.nubia.admin.application.controller;

import cn.nubia.admin.api.zuimeia.bean.Apps;
import cn.nubia.admin.api.zuimeia.service.AppsService;
import cn.nubia.admin.application.bean.Soft;
import cn.nubia.admin.application.bean.SoftEvaluating;
import cn.nubia.admin.application.bean.SoftEvaluatingVO;
import cn.nubia.admin.application.query.SoftEvaluatingQuery;
import cn.nubia.admin.application.query.SoftQuery;
import cn.nubia.admin.application.service.SoftEvaluatingService;
import cn.nubia.admin.application.service.SoftService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.sysconfig.bean.CategoryConstruct;
import cn.nubia.admin.sysconfig.service.CategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
 * Created by cnJason on 2015/7/22.
 */
@Controller
@RequestMapping(value = "/admin/soft/evaluating")
public class SoftEvaluatingController {

    @Autowired
    private SoftEvaluatingService softEvaluatingService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    SoftService softService;

    @Autowired
    AppsService appsService;


    @RequiresPermissions(value = "soft:rec:listSoftEvaluating")
    @RequestMapping(value = "listSoftEvaluating", method = RequestMethod.GET)
    public ModelAndView listSoftEvaluating(SoftEvaluatingQuery softEvaluatingQuery) {
        ModelAndView modelAndView = new ModelAndView("/app/softEvaluatingList");
        Paged<SoftEvaluatingVO> softEvaluatings = softEvaluatingService.findList(softEvaluatingQuery);
        modelAndView.addObject("softEvaluatings", softEvaluatings);
        modelAndView.addObject("query", softEvaluatingQuery);
        return modelAndView;
    }


    @RequestMapping(value = "viewSoftEvaluating", method = RequestMethod.GET)
    public ModelAndView viewSoft(HttpServletRequest request, SoftEvaluatingQuery softEvaluatingQuery) throws IOException {
        ModelAndView modelAndView = new ModelAndView("/app/editSoftEvaluating");

        SoftEvaluatingVO vo = new SoftEvaluatingVO();

        if (softEvaluatingQuery.getFromMark() != null) {
            Apps apps = null;
            if (softEvaluatingQuery.getFromMark().equalsIgnoreCase("zuimeia")) {
                apps = appsService.getApp();

            } else if (softEvaluatingQuery.getFromMark().equalsIgnoreCase("zuimeig")) {
                //最美游戏.
                apps = appsService.getGame();
            }
            if (apps != null) {
                //转换对象.
                vo = new SoftEvaluatingVO();
                vo.setRefererUrl(apps.getArticle_url());
                vo.setContent(apps.getContent());
                vo.setPicUrl(apps.getCover_image());
                vo.setSummary(apps.getDigest());
                vo.setTitle(apps.getTitle());
                vo.setCategoryId(apps.getCategory());
                vo.setLogo(apps.getIcon_image());
                if(apps.getPackage_name()!=null && !"".equals(apps.getPackage_name())){
                    Soft soft = softService.getSoftByPackageName(apps.getPackage_name());
                    if(soft!=null)vo.setSoftId(soft.getId());
                }
            }
        }
        if (softEvaluatingQuery.getId() != null) {
            SoftEvaluating softEvaluating = softEvaluatingService.get(softEvaluatingQuery.getId());
            if (softEvaluating != null) {
                BeanUtils.copyProperties(softEvaluating, vo);
            }
        }
        SoftQuery softQuery = new SoftQuery();
        softQuery.setId(vo.getSoftId());
        modelAndView.addObject("checkSofts", softService.findSoftList(softQuery));

        //展示分类信息
        if(vo.getCategoryId()!=null && vo.getCategoryId()>0){
            CategoryConstruct categoryConstruct = categoryService.getCategoryConstruct(vo.getCategoryId());
            vo.setFirstCategoryId(categoryConstruct.getFisrtCategoryId());
            vo.setSecCategoryId(categoryConstruct.getSecCategoryId());
            vo.setThirdCategoryId(categoryConstruct.getThirdCategoryId());
            setCategoryList(vo.getFirstCategoryId(),vo.getSecCategoryId(),modelAndView);
        }else{
            setCategoryList(0, null, modelAndView);
        }
        modelAndView.addObject("vo", vo);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = "save")
    public Response<Boolean> saveSoftEvaluating(@Valid SoftEvaluating softEvaluating, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.getFailedResponse(bindingResult.toString());
        }
        softEvaluating.setSoftId(softEvaluating.getSoftradio());
        softEvaluating.initCategoryId();
        softEvaluatingService.save(softEvaluating);
        return Response.getSuccessResponse(true);
    }

    @ResponseBody
    @RequestMapping(value = "hide")
    public boolean hide(Integer id) {
        softEvaluatingService.setState(id, SoftEvaluating.STATES_HIDDEN);
        return true;
    }

    @ResponseBody
    @RequestMapping(value = "show")
    public boolean show(Integer id) {
        softEvaluatingService.setState(id, SoftEvaluating.STATES_SHOW);
        return true;
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public boolean delete(Integer id) {
        softEvaluatingService.setState(id, SoftEvaluating.STATES_DEL);
        return true;
    }

    private void setCategoryList(Integer firtCategoryId, Integer secCategoryId, ModelAndView modelAndView) {
        modelAndView.addObject("firstCatagories", categoryService.getSubCategoryList(0));
        if (firtCategoryId != null && firtCategoryId > 0) {
            modelAndView.addObject("secondCatagories", categoryService.getSubCategoryList(firtCategoryId));
            if (secCategoryId != null && secCategoryId > 0) {
                modelAndView.addObject("thirdCatagories", categoryService.getSubCategoryList(secCategoryId));
            }
        }
    }

}
